package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Project;
import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.service.api.AuthProjectService;
import com.atguigu.crowd.service.api.ProjectMangerService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

//项目流程管理
@Controller
public class ProjectMangerHandler {
    @Autowired
    private AuthProjectService authProjectService;

    @Autowired
    private ProjectMangerService projectMangerService;




    //从项目表获取信息
    @RequestMapping("/project/process/page.html")
    public String getProjectProcessInfo(//keyword有时有 有时无 设置默认为空字符串
                                        @RequestParam(value = "keyword",defaultValue = "") String keyword,
                                        //pageNum 默认值设为1
                                        @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                        //pageSize 默认值设为5
                                        @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                        HttpSession session,
                                        ModelMap map
    ){
        //获取经过审核 且通过的项目 进行项目流程管理
        PageInfo<Project> ProjectPageInfo = authProjectService.getProjectPageInfoForProjectManger(keyword, pageNum, pageSize);

        System.out.println("获取的ProjectPageInfo "+ProjectPageInfo);

        List<Project> list = ProjectPageInfo.getList();
        System.out.println("获取的list "+list);
        //循环遍历 设置每个项目的状态文本
        for (int i = 0; i <list.size() ; i++) {

            Project p = list.get(i);
//            System.out.println("获取的p "+p.toString());
            Integer status=p.getStatus();
//            System.out.println("获取的p.getStatus() "+p.getStatus());
            switch (status){
                case 0:
                    p.setStatusText("众筹中");
                    break;
                case 1:
                    p.setStatusText("项目开发阶段1");
                    break;
                case 2:
                    p.setStatusText("项目开发阶段2");
                    break;
                case 3:
                    p.setStatusText("项目开发阶段3");
                    break;
                case 4:
                    p.setStatusText("项目开发阶段4");
                    break;
                case 5:
                    p.setStatusText("众筹成功");
                    break;
                case 6:
                    p.setStatusText("众筹失败");
                    break;
                case 7:
                    p.setStatusText("项目成功");
                    break;
                case 8:
                    p.setStatusText("项目失败");
                    break;
                default:
                    break;
            }

            list.set(0,p);
        }
        ProjectPageInfo.setList(list);
        System.out.println("项目状态文本："+ProjectPageInfo.getList().get(0).getStatusText());
        session.setAttribute("project_processInfo",ProjectPageInfo);
        map.addAttribute("project_processInfo",ProjectPageInfo);
        return "project-process";
    }


    //对项目流程进行管理 目前只允许对项目状态进行修改 且修改需要在范围内进行修改
    //是否添加只能项目经理进行修改？不进行这个操作 可以使用springsecurity进行限制 权限审核
    @RequestMapping("/project/process/update/page.html")
    public String updateProjectProcess(
            @RequestParam(value = "projectId",required = false) Integer projectId,
            HttpSession session
    ){
        PageInfo<Project> projectProcess = (PageInfo<Project>)session.getAttribute("project_processInfo");

        //可能还需要回报信息 详情图片等 可以在这里进行数据库操作
        List<Project> list = projectProcess.getList();
        for (Project p: list) {
            if(p.getId() == projectId){
                session.setAttribute("projectProcessSingle",p);
            }
        }
        return "project-process-update";
    }

    //进行修改
    @RequestMapping("/project/process/doUpdate.html")
    public String updateProjectProcess(@RequestParam("selectId")String selectId,
                                                     @RequestParam("projectId")String projectId,
                                                     @RequestParam("memberId")String memberId,
                                       HttpSession session){
        System.out.println("选中的是 "+selectId+" "+projectId);
        Integer selectVal=Integer.parseInt(selectId);
        Project projectS = (Project) session.getAttribute("projectProcessSingle");
        if(selectVal-projectS.getStatus()!=1){
            //填入的修改阶段 不满足条件 只能再原阶段基础上改一个阶段
            return "project-update-fail";
        }
        //如果此项目开发失败 需要memeber的failedNum+1
        if(selectVal==8){
            projectMangerService.updateMemberForFailed(Integer.parseInt(memberId));
        }

        projectMangerService.updateProject(selectVal,Integer.parseInt(projectId));
        return "redirect:/project/process/page.html";
    }
}
